﻿export function render_list(list,fn) {
	var vnode=[];
	try{
		if(list.length==0){
			return vnode;
		}
		list.forEach((item,index)=>{
			vnode.push(fn(item,index));
		});
	}catch(e){
	}
	return vnode;
}
export function render_obj(obj,fn){
	var piece=[];
	for(let prop in obj){
		piece.push(fn(prop,obj[prop]));
	}
	return piece;
}
export function getNodeByPath(nodeTree,path){
	var tem=nodeTree;
	for(let i in path){
		if(i==0){
			tem=tem[path[i]];
		}else{
			tem=tem.children[path[i]]
		}
		console.log(tem);
	}
	for(let i=0;i<path.lenth;i++){
	}
	return tem;
}
export function sizeToNum(sizeStr){
	var sizeStr= sizeStr.replace('px','');
	return parseFloat(sizeStr);
}
export function strToNum(str,num){
	var num=num ? num:0;
	var value= parseFloat(str);
	return parseFloat(value.toFixed(num))
}
export function paseToNodeTree(data,fields,openIds){
	if(!data || data.length==0){
		return [];
	}
	var defaultFields={
		id:"id",
		pId:"pId"
	}
	var fields=Object.assign({},defaultFields,fields);
	console.log(fields);
	var path_store={},
		/*筛选出一级节点*/
		findAllroot=function(group,nodesAll,fields){
			var rootNodes={}
			for(let key in group){
				var test=true
				for(let i=0;i<nodesAll.length;i++){
					var isSelf=false
					for(let j=0;j<group[key].length;j++){
						isSelf=group[key][j][fields.id]==nodesAll[i][fields.id]
						if(isSelf)break
					}
					if(isSelf)continue
					test= test && "key_"+nodesAll[i][fields.id]!=key
				}
				if(test){
					rootNodes[key+""]=group[key]
				}
			}
			return rootNodes
		},
		/*渲染节点为虚拟DOM*/
		vNodeRender=function(dataList,vNodeGroup,path){
			for(let index=0;index<dataList.length;index++){
				var item=dataList[index];
				if(path.length==0){
					path_store["key_"+dataList[index].id]=[index];
					if(path){
						dataList[index].level=path.length+1;
					}
				}else{
					dataList[index].isLeaf=!hasChild(dataList[index],vNodeGroup);
					if(path){
						dataList[index].level=path.length+1;
					}
					if(path){
						path_store["key_"+dataList[index].id]=path.concat(index);
					}
				}
			}
		},
		/*设置展开至顶级节点*/
		openToRoot=function(nodeTree,ids){
			ids.forEach((id,index)=>{
				var tem=nodeTree;
				var path=path_store["key_"+id];
				if(path){
					path.forEach((item,index)=>{
						if(index==0){
							tem=tem[item];
						}else{
							tem=tem.children[item];
						}
						tem.open=true
					})
				}
			})
		},
		/*把节点以parentId分组*/
		getNodeGroup=function(nodeList){
			var group={};
			for(let i=0;i<nodeList.length;i++){
				nodeList[i].open=false;
				group["key_"+nodeList[i][fields.pId]]=[];
			}
			for(let i=0;i<nodeList.length;i++){
				group["key_"+nodeList[i][fields.pId]].push(nodeList[i]);
			}
			return group;
		},
		getByPath=function(vNode,path){
			var tem=vNode;
			path.forEach(function(item,index){
				if(index==0){
					tem=tem[item];
				}else{
					tem=tem.children[item];
				}
			});
			return tem;
		},
		hasChild=function(node,vNodeGroup){
			if(vNodeGroup){
				return vNodeGroup.hasOwnProperty("key_"+node.id);
			}else{
				return false;
			}
		},
		main=function(data){
			if(!data || data.length==0){
				return [];
			}
			var _this=this;
			var rootList=[],
				vNode=[],
				vNodeGroup={};
			vNodeGroup=getNodeGroup(data);
			//获取一级节点
			rootList=findAllroot(vNodeGroup,data,fields);
			//挂载一级级节点
			for(let prop in rootList){
				vNodeRender(rootList[prop],vNodeGroup,[]);
				vNode=vNode.concat(rootList[prop]);
			}
			//删除一级节点，因为一级节点已经进入虚拟DOM树了
			for(let prop in rootList){
				delete vNodeGroup[prop]
			}
			var usedNode=[];
			/*第一层循环用于保证分组列表里面的每一组都在整个的节点集合中寻找它的父级，
		循环退出的条件是，分组列表里的每一项在当前的路径记录都没有对应的父节点记录*/
			do{
				usedNode=[];
				for(let prop in vNodeGroup){
					//如果
					if(path_store.hasOwnProperty(prop)){
						vNodeRender(vNodeGroup[prop],vNodeGroup,path_store[prop]);
						var targetNode=getByPath(vNode,path_store[prop]);
						usedNode.push(prop);
						if(targetNode){
							targetNode.children=vNodeGroup[prop];
						}
					}
				}
				usedNode.forEach(function(item){
					delete vNodeGroup[item];
				})
			}while(usedNode.legnth>0)
			/*
		for(let prop in vNodeGroup){
			var children2 =vNodeRender(vNodeGroup[prop],vNodeGroup,[]);
			console.log(children2);
			vNode.push(children2);
		}
		*/
			return vNode;
		};
	return main(data)
}
export function condition(cd,fn){
	return fn(cd)
}
export function fn(_fn){
	return _fn()
}
export function unique(array){ 
	console.log("uni 1",array);
	var n = {}, r = [], len = array.length, val, type; 
	for (var i = 0; i < array.length; i++) { 
		val = array[i]; 
		type = typeof val; 
		if (!n[val]) { 
			n[val] = [type]; 
			r.push(val); 
		} else if (n[val].indexOf(type) < 0) { 
			n[val].push(type); 
			r.push(val); 
		} 
	} 
	console.log("unique",r);
	return r; 
}
export function arrayEach(arr,fn){
	var vnode=[];
	var node={};
	arr.forEach((item,index)=>{
		node=fn(item,index);
		vnode.push(node);
	});
	return vnode;
}
